package com.bidevalution.business.impl;

import com.bidevalution.business.enums.VoteAlgorithmEnum;
import com.bidevalution.business.enums.VoteOptionEnum;
import com.bidevalution.business.enums.VoteTypeEnum;
import com.bidevalution.business.service.BeExpertVoteService;
import com.bidevalution.business.service.BeSectionExpertInfoService;
import com.bidevalution.business.service.VoteUnitNumService;
import com.bidevalution.dao.entity.BeExpertVoteDetailExt;
import com.bidevalution.dao.entity.BeExpertVoteExt;
import com.bidevalution.dao.entity.BeSectionEnterprise;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.dao.mapper.BeBidResultMapper;
import com.bidevalution.dao.mapper.BeSectionEnterpriseMapper;
import com.qtp.core.model.Response;
import com.qtp.core.util.GsonUtil;
import com.qtp.core.util.OConvertUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description 处理业务功能
 * @Date 2020-04-16 13:47
 * @Author lhl
 */
@Log4j2
@Service
public class VoteUnitNumServiceImpl implements VoteUnitNumService {

    @Autowired(required = false)
    BeBidResultMapper beBidResultMapper;
    @Autowired(required = false)
    BeSectionEnterpriseMapper beSectionEnterpriseMapper;
    @Autowired
    BeExpertVoteService beExpertVoteService;
    @Autowired
    BeSectionExpertInfoService beSectionExpertInfoService;

    @Override
    public void checkInitiateVote(String sectionId, String sectionStepId, int warnUnitNum, int isProcessBefore) {
        log.info("LOG[]checkInitiateVote[]{},params={}", "记录日志",GsonUtil.toJsonWithGson(sectionId)+";"+GsonUtil.toJsonWithGson(sectionStepId)+";"+GsonUtil.toJsonWithGson(warnUnitNum));
        try {
            if (isProcessBefore == 1){
                List<BeSectionEnterprise> beSectionEnterprises = beSectionEnterpriseMapper.selectBySectionId(sectionId);
                if (beSectionEnterprises.size() >= warnUnitNum){
                    return;
                }
            }else {
                Map<String, Object> passedUnitNum = beBidResultMapper.getPassedUnitNum(sectionStepId);
                //通过的单位数
                int passed = OConvertUtils.getInt(passedUnitNum.get("passed"));
                //未通过的单位数
                int notPassed = OConvertUtils.getInt(passedUnitNum.get("notPassed"));
                //是否流程第一环节
                if (!(passed < warnUnitNum && notPassed > 0)){
                    return;
                }
            }
            Response response = callForVote(sectionId,sectionStepId, warnUnitNum);
            log.info("LOG[]checkInitiateVote1[]{},response={},params={}", "记录日志",GsonUtil.toJson(response),GsonUtil.toJsonWithGson(sectionId)+";"+GsonUtil.toJsonWithGson(sectionStepId)+";"+GsonUtil.toJsonWithGson(warnUnitNum));
            if (!response.isSuccess()){
                throw new RuntimeException(response.getError());
            }
        }catch (Exception e){
            log.error("LOG[]checkInitiateVote[]{},error={},params={}","异常",GsonUtil.toJsonWithGson(e),GsonUtil.toJsonWithGson(sectionId)+";"+GsonUtil.toJsonWithGson(sectionStepId)+";"+GsonUtil.toJsonWithGson(warnUnitNum));
            throw e;
        }
    }
    /**
     * @Description 发起表决
     * @param sectionId     标段id
     * @param warnUnitNum   单位基数
     * @date 2020-04-15 14:39
     * @author lhl
     */
    private Response callForVote(String sectionId, String sectionStepId, int warnUnitNum){
        String title = String.format("小于%s家，仍有竞争力，继续评审", warnUnitNum);
        BeExpertVoteExt expertVote = new BeExpertVoteExt();
        try {
            List<BeExpertVoteExt> expertVoteList = new ArrayList<>();
            expertVote.setSectionId(sectionId);
            expertVote.setSectionStepId(sectionStepId);
            expertVote.setTitle(title);
            expertVote.setContent(title);
            expertVote.setVoteOptions(GsonUtil.toJson(VoteOptionEnum.getNames()));
            expertVote.setVoteType(VoteTypeEnum.Automatic.getCode());
            expertVote.setVoteAlgorithm(VoteAlgorithmEnum.ShaoShuFuCongDuoShu.code);
            expertVote.setOriginator("SYSTEM");
            expertVote.setNextJob("unitNumCheckNextJob");
            Map<String,Object> voteParams = new HashMap<String,Object>();
            voteParams.put("sectionId", sectionId);
            expertVote.setVoteParams(GsonUtil.toJson(voteParams));
            List<BeSectionExpertInfo> expertInfoList = beSectionExpertInfoService.getListBySectionId(sectionId, "0");
            List<BeExpertVoteDetailExt> voteDetailList = new ArrayList<>();
            BeExpertVoteDetailExt voteDetail;
            for (BeSectionExpertInfo expertInfo : expertInfoList) {
                voteDetail = new BeExpertVoteDetailExt();
                voteDetail.setExpertId(expertInfo.getSeqId());
                voteDetailList.add(voteDetail);
            }
            expertVote.setVoteDetailList(voteDetailList);
            expertVoteList.add(expertVote);
            return beExpertVoteService.addForVote(expertVoteList);
        } catch (Exception e) {
            log.error("LOG[]callForVote_99[]{},error={}","异常日志",GsonUtil.toJsonWithGson(e));
            return Response.fail("发起表决失败！");
        }
    }

}
